package com.kun.gulimall.guliproduct.controller;

import com.kun.gulimall.guliproduct.query.CategoryQuery;
import com.kun.gulimall.guliproduct.query.CategorySort;
import com.kun.gulimall.guliproduct.service.PmsCategoryService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@AllArgsConstructor
public class CategoryController {

    private PmsCategoryService categoryService;

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @GetMapping("/list/tree")
    public ResponseEntity<Object> list() {
        return ResponseEntity.ok(categoryService.listWithTree());
    }

    /**
     * 删除菜单
     */
    @DeleteMapping
    public ResponseEntity<Object> delete(Long[] catIds) {
        return ResponseEntity.ok(categoryService.removeMenuByIds(catIds));
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody CategoryQuery pmsCategory) {
        return ResponseEntity.ok(categoryService.saveCategory(pmsCategory));
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Object> update(@RequestBody CategoryQuery pmsCategory) {
        return ResponseEntity.ok(categoryService.updateCategory(pmsCategory));
    }

    /**
     * 修改集合
     */
    @PutMapping("/update_list")
    public ResponseEntity<Object> updateList(@RequestBody List<CategorySort> categorySorts) {
        return ResponseEntity.ok(categoryService.updateCategoryBatchById(categorySorts));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Object> get(@PathVariable Long id) {
        return ResponseEntity.ok(categoryService.getCategory(id));
    }
}
